package com.frostmourne.mxsattributeinventory.utils;

import com.frostmourne.mxsattributeinventory.MXSAttributeInventory;
import com.frostmourne.mxsattributeinventory.hooks.*;
import com.gitee.mxstars.mxscript.MXScriptExtra;
import com.gitee.mxstars.sql.STable;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;

public class FileUtils {
    public static List<File> getFiles(String path){
        List<File> files = new ArrayList<>();
        for (File f : Objects.requireNonNull(new File(path).listFiles())){
            if (f.getName().contains(".yml")){
                files.add(f);
            }
            if (f.isDirectory()){
                files.addAll(getFiles(f.getPath()));
            }
        }
        return files;
    }

    public static void createData(Player player) throws IOException, InvalidConfigurationException {
        File file = new File(MXSAttributeInventory.savePath , player.getName() + ".yml");
        if (!file.exists()){
            FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
            fileConfiguration.set("InvData",new ArrayList<>());
            fileConfiguration.save(file);
        }else{
           refresh(player);

        }
    }
    public static File getData(Player player) {
        File file = new File(MXSAttributeInventory.savePath , player.getName() + ".yml");
        if (file.exists()){
            return file;
        }
        return null;
    }

    public static void refresh(Player player){
        File f = getData(player);

        switch (MXSAttributeInventory.ATTR.toUpperCase()){
            case "ATTRIBUTEPLUS":

                if (f != null) {
                    for (String invName : MXSAttributeInventory.inventoryMap.keySet()){
                        if (!MXSAttributeInventory.enableMap.get(invName).contains("Attribute")){
                            continue;
                        }
                        List<String> invAttr = new ArrayList<>();
                        for (int i = 0; i < MXSAttributeInventory.inventoryMap.get(invName).getInv().getSize(); i++) {
                            ItemStack item = getItemByNameAndSlot(player,invName,i);
                            if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()){
                                invAttr.addAll(item.getItemMeta().getLore());
                            }
                        }
                        APHook.setAttr(invName,player,invAttr);
                    }
                }
                break;
            case "SX-ATTRIBUTE":
                if (f != null) {
                    for (String invName : MXSAttributeInventory.inventoryMap.keySet()){
                        if (!MXSAttributeInventory.enableMap.get(invName).contains("Attribute")){
                            continue;
                        }
                        List<String> invAttr = new ArrayList<>();
                        for (int i = 0; i < MXSAttributeInventory.inventoryMap.get(invName).getInv().getSize(); i++) {
                            ItemStack item = getItemByNameAndSlot(player,invName,i);
                            if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()){
                                invAttr.addAll(item.getItemMeta().getLore());
                            }
                        }
                        SXHook.setAttr(invName,player,invAttr);
                    }
                }
                break;
            case "ITEMLOREORIGIN":

                if (f != null) {
                    for (String invName : MXSAttributeInventory.inventoryMap.keySet()){
                        if (!MXSAttributeInventory.enableMap.get(invName).contains("Attribute")){
                            continue;
                        }
                        List<String> invAttr = new ArrayList<>();
                        for (int i = 0; i < MXSAttributeInventory.inventoryMap.get(invName).getInv().getSize(); i++) {
                            ItemStack item = getItemByNameAndSlot(player,invName,i);
                            if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()){
                                invAttr.addAll(item.getItemMeta().getLore());
                            }
                        }
                        ILOHook.setAttr( invName,player,invAttr);
                    }
                }
                break;
            case "ORIGINATTRIBUTE":

                if (f != null) {
                    for (String invName : MXSAttributeInventory.inventoryMap.keySet()){
                        if (!MXSAttributeInventory.enableMap.get(invName).contains("Attribute")){
                            continue;
                        }
                        List<String> invAttr = new ArrayList<>();
                        for (int i = 0; i < MXSAttributeInventory.inventoryMap.get(invName).getInv().getSize(); i++) {
                            ItemStack item = getItemByNameAndSlot(player,invName,i);
                            if (item != null && item.hasItemMeta() && item.getItemMeta().hasLore()){
                                invAttr.addAll(item.getItemMeta().getLore());
                            }
                        }
                        OAHook.setAttr( invName,player,invAttr);
                    }
                }
                break;
            default:
                break;
        }
        for (String s : MXSAttributeInventory.inventoryMap.keySet()){
            if (!MXSAttributeInventory.isHasDA()){
                return;
            }
            if (MXSAttributeInventory.enableMap.get(s).contains("DragonArmourers")){
                DAHook.refreshArmourers(player,s);
            }
        }

    }

    public static ItemStack getItemByNameAndSlot(Player player, String invName, int slot) {
        File f = getData(player);
        if (f != null){
            FileConfiguration data = YamlConfiguration.loadConfiguration(f);
            if (    data.getConfigurationSection("InvData") != null
                && data.getConfigurationSection("InvData").getKeys(false).size() != 0 &&
                    data.getConfigurationSection("InvData").getKeys(false).contains(invName)
            && data.getConfigurationSection("InvData." + invName).getKeys(false).contains(String.valueOf(slot))
            ){
                return data.getItemStack("InvData." + invName + "." + slot);
            }
        }
        return null;
    }

    public static void saveDataToFile(Player player,String invName, Inventory inventory){
        File f = getData(player);
        if (f != null) {
            FileConfiguration data = YamlConfiguration.loadConfiguration(f);
            if (!MXSAttributeInventory.inventoryMap.containsKey(invName)){
                return;
            }
            if (    data.getConfigurationSection("InvData") != null &&
                    data.getConfigurationSection("InvData").getKeys(false).contains(invName)){
                data.set("InvData." + invName,null);
            }
            Inventory originInv = MXSAttributeInventory.inventoryMap.get(invName).getInv();
            for (int i = 0; i < inventory.getSize(); i++) {
                ItemStack item = inventory.getItem(i);
                if ((item != null) && !(originInv.getItem(i).isSimilar(item))){
                    data.set("InvData." + invName + "." + i,item);
                }
            }
            try {
                data.save(f);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (MXSAttributeInventory.enableMap.get(invName).contains("Attribute")) {
                refresh(player);
            }
            if (MXSAttributeInventory.isHasDA() && MXSAttributeInventory.enableMap.get(invName).contains("DragonArmourers")) {
                DAHook.refreshArmourers(player,invName);
            }
        }

    }

    public static List<ItemStack> getAllItems(Player player){
        List<ItemStack> items = new ArrayList<>();
        File f = getData(player);
        if (f != null) {
            for (String invName : MXSAttributeInventory.inventoryMap.keySet()){
                for (int i = 0; i < MXSAttributeInventory.inventoryMap.get(invName).getInv().getSize(); i++) {
                    ItemStack item = getItemByNameAndSlot(player,invName,i);
                    if (item != null){
                        items.add(item);
                    }
                }
            }
        }
        return items;
    }

    public static void openInventory(Player player,String invName){
        if (MXSAttributeInventory.inventoryMap.containsKey(invName)){
            Inventory originalInventory = MXSAttributeInventory.inventoryMap.get(invName).getInv();
            Inventory inventory = Bukkit.createInventory(null,originalInventory.getSize(),originalInventory.getTitle());
            for (int i = 0; i < originalInventory.getSize(); i++) {
                inventory.setItem(i,originalInventory.getItem(i));
            }
            for (int i = 0; i < inventory.getSize(); i++) {
                ItemStack item = getItemByNameAndSlot(player,invName,i);
                if (item != null){
                    inventory.setItem(i,item);
                }
            }

            player.openInventory(inventory);
        }
    }


}
